#ifndef __M510T_DRIVER_H__
#define __M510T_DRIVER_H__
#include <stdio.h>
#include "driver_gpio.h"
#include "sys_types.h"
#include "app_config.h"

typedef int (*m510_button_action_func)(void);
#define BUTTON_PRESS_TIMES 5
#define BUTTON_DOUBLE_PRESS_TIMES 20
#define BUTTON_LONG_PRESS_TIMES 200
#define BUTTON_VLONG_PRESS_TIMES 500
#ifdef AUDIO_TX

#define NS_ONOFF_KEY GPIO24 //one click
#define M510_KEY2 GPIO14    //voice add
#define M510_KEY3 GPIO13    //voice sub
// #define MODE_CHANGE_KEY GPIO15
#define PAIR_KEY GPIO24 //three click
#define M510_KEY1 GPIO15

#define BT_STATE GPIO11
#define M510T_BUTTON_NUM 7

#define M510_LED1 GPIO32 //tx connect
// #define M510_LED2 GPIO32 //
#define M510_LED3 GPIO33 //live
#define M510_LED4 GPIO33 //vlong
// #define M510_LED5 GPIO17
enum COLOR_LED
{
    ENUM_LED1,
    // ENUM_LED2,
    ENUM_LED3,
    ENUM_LED4,
    ENUM_LED5,
};
#define M510T_LED_NUM 3
#else
#define M510_KEY1 GPIO15
#define M510T_BUTTON_NUM 1
#define M510_LED1 GPIO33
#define M510_LED2 GPIO32

enum COLOR_LED
{
    ENUM_LED1,
    ENUM_LED2,
    ENUM_LED3,
    ENUM_LED4,
    ENUM_LED5,
};
#define M510T_LED_NUM 2
#endif

#define PARING_TIMEOUT 20000  // 20S
#define DISCONNECT_TIME 60000 // 10min

enum
{
    M510T_LED_ON,
    M510T_LED_OFF,
    M510T_LED_ALTER,
    M510T_LED_FLICK,
};

typedef struct
{
    uint8_t led_gpio_port;
    uint8_t led_onoff_flag;
    uint16_t led_on_time;
    uint16_t led_off_time;
    uint64_t led_prev_count;
    uint16_t led_curn_count;
    uint16_t led_time_out;
    uint16_t led_time_out_count;
} m510t_led_info;

enum
{
    M510T_BUTTON_NULL,
    M510T_BUTTON_PRESS_DOWN,
    M510T_BUTTON_DOUBLE_PRESS_DOWN,
    M510T_BUTTON_PRESS_UP,
    M510T_BUTTON_DOUBLE_PRESS,
    M510T_BUTTON_LONG_PRESS,
    M510T_BUTTON_LONG_PRESS_UP,
    M510T_BUTTON_VLONG_PRESS,
};

enum
{
    M510T_LED_NULL,
    M510T_LED_PARING,
    M510T_LED_DISCONNECT,
    M510T_LED_NS_ON,
    M510T_LED_NS_OFF,
    M510T_LED_START_RECORD,
    M510T_LED_STOP_RECORD,
    M510T_LED_POWER_DOWN,
    M510T_LED_POWER_ON,
    M510T_LED_LOW_POWER,
    M510T_LED_CHARGING,
    M510T_LED_CHARG_FULL,
    M510T_LED_CHARG_OUT,
    M510T_LED_NOISE_REDUCTION,
    M510T_LED_REVEB_ON,
    M510T_LED_REVEB_OFF,
    M510T_LED_EXTERN_BT_ON,
    M510T_LED_EXTERN_BT_OFF,
    M510T_LED_TX_CONNECT,
    M510T_LED_TX_UNCONNECT,
    M510T_LED_TX_TIME_OUT,
    M510T_LED_RX_CONNECT,
    M510T_LED_RX_UNCONNECT,
};
typedef struct _IOKeyMsg
{
    uint32_t button_gpio : 16;
    uint32_t button_mode : 16;
} IOKeyMsg;

typedef enum _IOKeyType
{
    IO_KEY_UNKOWN_TYPE = 0,
    IO_KEY_PRESSED,
    IO_KEY_RELEASED,
    IO_KEY_LONG_PRESSED,
    IO_KEY_LONG_PRESS_HOLD,
    IO_KEY_LONG_LONG_PRESS_HOLD,
    IO_KEY_LONG_RELEASED,
    IO_KEY_RELEASED_2,
    IO_KEY_RELEASED_3,
    IO_KEY_RELEASED_4,
} IOKeyType;

typedef enum _IO_KEY_STATE
{
    IO_KEY_STATE_IDLE,
    IO_KEY_STATE_JITTER,
    IO_KEY_STATE_PRESS_DOWN,
    IO_KEY_STATE_CP

} IO_KEY_STATE;

#define IO_KEY_SCAN_TIME 2
#define IO_KEY_JITTER_TIME 3
#define IO_KEY_CP_TIME 100   //for hold or long press
#define IO_KEY_CPH_TIME 20   //for long long press
#define IO_KEY_LONG_TIME 200 //for long long press

typedef struct
{
    uint16_t button_gpio;
    uint16_t button_mode;
    m510_button_action_func button_handle;
} m510t_button_info;
typedef struct _TIMER
{
    uint64_t TimeOutVal;   //time out value
    uint64_t TickValCache; //soft timer setting value
    uint8_t IsTimeOut;     //time out flag
} TIMER;
void m510t_Led_init();
void m510t_Led_process();
void m510t_Button_init();
void m510t_other_init();
int m510t_Button_process();
uint16_t m510t_Button_scan();
IOKeyMsg IOKeyScan(void);
void set_Power_led(BOOL Powerflag);
void set_Reverb_led(BOOL ReverbLevel);
void set_mode_led(uint8_t mode);
void set_ns_led(void);
void set_paring_flag(int paring);
int get_paring_flag(void);
void button_set_led(uint8_t event);
void button_connect_led(uint8_t flag);
void set_charging_flag(int charging);
void m510t_rf_status(uint8_t status);
IOKeyMsg ReadKeyclick(IOKeyMsg Msg);
uint8_t get_charging_flag();
typedef enum _PWR_LEVEL
{
    PWR_LEVEL_0 = 0,
    PWR_LEVEL_1,
    PWR_LEVEL_2,
    PWR_LEVEL_3,
    PWR_LEVEL_4 //max
} PWR_LEVEL;
#define PWR_LEVEL1_DATA 3500
#define PWR_LEVEL2_DATA 3700
#define PWR_LEVEL3_DATA 3900
#define PWR_LEVEL4_DATA 4100

#endif
